package com.dbuil.bleshoes.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dbuil.bleshoes.MainActivity;
import com.dbuil.bleshoes.ui.view.CircleTimerView;
import com.gyf.immersionbar.ImmersionBar;
import com.permissionx.guolindev.PermissionX;

import com.dbuil.bleshoes.R;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SplashActivity extends AppCompatActivity {


    @BindView(R.id.tv_mills)
    TextView tvMills;

    private Timer timer;

    private int mills = 3; //倒计时5s
    private CircleTimerView mCircleTimerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!isTaskRoot()) {
            finish();
            return;
        }

        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);

        ImmersionBar
                .with(this)
                .transparentBar()     //透明状态栏和导航栏，不写默认状态栏为透明色，导航栏为黑色（设置此方法，fullScreen()方法自动为true）
                .init();


        tvMills.setText(mills + "s");

        getPermission();


    }


    private void initCircleImageview() {

        mCircleTimerView = (CircleTimerView) findViewById(R.id.circle_timer);
        mCircleTimerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击跳过倒计时
                mCircleTimerView.cancelAnim();
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        });
        mCircleTimerView.setOnCountDownFinish(new CircleTimerView.OnCountDownFinish() {
            @Override
            public void onFinish() {
                //倒计时结束
                ToastUtils.showLong("倒计时结束");

            }
        });
        mCircleTimerView.start();
    }


    /**
     * 申请权限
     */
    private void getPermission() {

        PermissionX.init(this)
                .permissions(Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                .setDialogTintColor(Color.parseColor("#333333"), Color.parseColor("#ffffff"))//第一个参数是设置浅色主题下的颜色值，第二个参数是设置深色主题下的颜色值
                .explainReasonBeforeRequest()//先弹出一个权限列表的提示
                .onExplainRequestReason((scope, deniedList, beforeRequest) -> {
                    scope.showRequestReasonDialog(deniedList, "DBUIL需要使用以下权限才能正常使用:", "好的", "稍后提示");//按钮可以设置一个或者两个
                })
                .onForwardToSettings((scope, deniedList) -> scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白"))
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        ToastUtils.showShort("所有申请的权限都已通过");
                        initCircleImageview();
                        timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {

                                mills--;
                                next();

                            }
                        }, 1000, 1000);

                    } else {

                        getPermission();
                        ToastUtils.showShort("您拒绝了如下权限:" + deniedList);
                    }

                });
    }

    private void next() {
        runOnUiThread(() -> {


            if (mills < 1) {
                ActivityUtils.startActivity(MainActivity.class);
                SplashActivity.this.finish();
                return;
            }

            tvMills.setText(mills + "s");

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }


}
